//
//  TotalAssets.m
//  MyWallet
//
//  Created by 小肥羊 on 15/7/15.
//  Copyright (c) 2015年 小肥羊. All rights reserved.
//

#import "TotalAssets.h"
#import "WalletDefaults.h"
#import "WalletLocalData.h"

#pragma mark - 卡模型
@implementation CardModel
- (id)init
{
    self = [super init];
    if (self) {
        self.cardName = @"未激活";
        self.cardMoney = 0.0f;
        self.isActivation = NO;
    }
    return self;
}

- (void)cancellation
{
    self.cardName = @"未激活";
    self.cardMoney = 0.0f;
    self.isActivation = NO;
}
@end


#pragma mark - 总资产模型
@implementation TotalAssets
#pragma mark - 单例
+ (TotalAssets *)sharedManager
{
    static TotalAssets *sharedAccountManagerInstance = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedAccountManagerInstance = [[self alloc] init];
        NSMutableDictionary *dict = [WalletDefaults getTotalAssetsDict];
        if (dict) {
            [sharedAccountManagerInstance upTotalAssets:[WalletDefaults getTotalAssetsDict]];
        }
    });
    return sharedAccountManagerInstance;
}

#pragma mark - 初始化
- (id)init
{
    self = [super init];
    if (self) {
        self.walletMoney = 0;
        self.card1 = [[CardModel alloc] init];
        self.card2 = [[CardModel alloc] init];
        self.card3 = [[CardModel alloc] init];
        self.card4 = [[CardModel alloc] init];
        self.card5 = [[CardModel alloc] init];
        self.card6 = [[CardModel alloc] init];
    }
    return self;
}

#pragma mark - Set 设置
- (void)setWalletMoney:(float)walletMoney
{
    _walletMoney = walletMoney;
    [self needUpTotalAssets];
}

#pragma mark - 转换
- (void)upTotalAssets:(NSMutableDictionary *)dict
{
    self.walletMoney = [dict[@"walletMoney"] floatValue];
    [self upCardToDict:self.card1 Dict:dict[@"card1"]];
    [self upCardToDict:self.card2 Dict:dict[@"card2"]];
    [self upCardToDict:self.card3 Dict:dict[@"card3"]];
    [self upCardToDict:self.card4 Dict:dict[@"card4"]];
    [self upCardToDict:self.card5 Dict:dict[@"card5"]];
    [self upCardToDict:self.card6 Dict:dict[@"card6"]];
}
- (void)upCardToDict:(CardModel *)card Dict:(NSMutableDictionary *)dict
{
    card.cardName = dict[@"cardName"];
    card.cardMoney = [dict[@"cardMoney"] floatValue];
    card.isActivation = [dict[@"isActivation"] boolValue];
}

#pragma mark - 公共方法
/*! @brief 获取卡 */
- (CardModel *)cardWithIndexString:(NSString *)indexString
{
    if ([indexString isEqualToString:@"1"]) {
        return self.card1;
    } else if ([indexString isEqualToString:@"2"]) {
        return self.card2;
    } else if ([indexString isEqualToString:@"3"]) {
        return self.card3;
    } else if ([indexString isEqualToString:@"4"]) {
        return self.card4;
    } else if ([indexString isEqualToString:@"5"]) {
        return self.card5;
    } else if ([indexString isEqualToString:@"6"]) {
        return self.card6;
    }
    return nil;
}
/*! @brief 计算总资产 */
- (float)totalAssets
{
    float totalAssets = 0;
    totalAssets = totalAssets + self.walletMoney;
    totalAssets = totalAssets + self.card1.cardMoney;
    totalAssets = totalAssets + self.card2.cardMoney;
    totalAssets = totalAssets + self.card3.cardMoney;
    totalAssets = totalAssets + self.card4.cardMoney;
    totalAssets = totalAssets + self.card5.cardMoney;
    totalAssets = totalAssets + self.card6.cardMoney;
    return totalAssets;
}

#pragma mark - 变值操作
- (BOOL)saveWallemoney:(float)money RecordDate:(NSDate *)date MoneyFor:(NSString *)moneyFor Remarks:(NSString *)remarks
{
    if (money == 0) {
        return NO;
    }
    if ([[WalletLocalData sharedManager] addRecord:date MoneyFrom:Macro_MoneyFromWallet CheckoutType:YES Money:money MoneyFor:moneyFor Remarks:remarks]) {
        self.walletMoney = self.walletMoney + money;
        [WalletDefaults saveTotalAssets:self];
        return YES;
    } else {
        return NO;
    }
}
- (BOOL)drawWallemoney:(float)money RecordDate:(NSDate *)date MoneyFor:(NSString *)moneyFor Remarks:(NSString *)remarks
{
    if (self.walletMoney < money || money == 0) {
        return NO;
    }
    if ([[WalletLocalData sharedManager] addRecord:date MoneyFrom:Macro_MoneyFromWallet CheckoutType:NO Money:money MoneyFor:moneyFor Remarks:remarks]) {
        self.walletMoney = self.walletMoney - money;
        [WalletDefaults saveTotalAssets:self];
        return YES;
    } else {
        return NO;
    }
}

/**
 *  激活卡
 *
 *  @param indexString    当前卡识别
 *  @param cardName       卡名称
 *  @param money          激活初始金额
 *  @param date           初始日期
 *  @param initialization 是:初始化;否:钱包转账
 */
- (BOOL)activationCard:(NSString *)indexString CardName:(NSString *)cardName Money:(float)money RecordDate:(NSDate *)date Initialization:(BOOL)initialization
{
    if (!initialization && self.walletMoney < money) {
        return NO;
    }
    CardModel *card = [self cardWithIndexString:indexString];
    card.cardName = cardName;
    card.cardMoney = money;
    card.isActivation = YES;
    if (initialization) {
        [[WalletLocalData sharedManager] addRecord:date MoneyFrom:card.cardName CheckoutType:YES Money:money MoneyFor:Macro_MoneyForDraw[0] Remarks:@"激活卡"];
    } else {
        [self drawWallemoney:money RecordDate:date MoneyFor:Macro_MoneyForChange Remarks:[NSString stringWithFormat:@"激活%@",cardName]];
        [[WalletLocalData sharedManager] addRecord:[date dateByAddingTimeInterval:1] MoneyFrom:card.cardName CheckoutType:YES Money:money MoneyFor:Macro_MoneyForChange Remarks:@"激活卡"];
    }
    [WalletDefaults saveTotalAssets:self];
    return YES;
}
/*! @brief 注销卡 */
- (BOOL)cancellationCard:(NSString *)indexString RecordDate:(NSDate *)date toWalletMoney:(BOOL)toWalletMoney
{
    CardModel *card = [self cardWithIndexString:indexString];
    if (toWalletMoney) {
        [self cardToPurse:indexString Money:card.cardMoney RecordDate:date];
        [[WalletLocalData sharedManager] addRecord:[date dateByAddingTimeInterval:2] MoneyFrom:card.cardName CheckoutType:NO Money:0 MoneyFor:Macro_MoneyForDraw[0] Remarks:@"注销卡"];
    } else {
        [[WalletLocalData sharedManager] addRecord:date MoneyFrom:card.cardName CheckoutType:NO Money:card.cardMoney MoneyFor:Macro_MoneyForDraw[0] Remarks:@"注销卡"];
    }
    [card cancellation];
    [WalletDefaults saveTotalAssets:self];
    return YES;
}

/*! @brief 刷卡消费 */
- (BOOL)drawCard:(NSString *)indexString Money:(float)money RecordDate:(NSDate *)date MoneyFor:(NSString *)moneyFor Remarks:(NSString *)remarks
{
    CardModel *card = [self cardWithIndexString:indexString];
    if (card.cardMoney < money) {
        return NO;
    }
    if ([[WalletLocalData sharedManager] addRecord:date MoneyFrom:card.cardName CheckoutType:NO Money:money MoneyFor:moneyFor Remarks:remarks]) {
        card.cardMoney = card.cardMoney - money;
        [WalletDefaults saveTotalAssets:self];
        return YES;
    } else {
        return NO;
    }
}

/*! @brief 取钱到钱包里 */
- (BOOL)cardToPurse:(NSString *)indexString Money:(float)money RecordDate:(NSDate *)date
{
    if ([self drawCard:indexString Money:money RecordDate:date MoneyFor:Macro_MoneyForChange Remarks:@"转到钱包中"]) {
        [self saveWallemoney:money RecordDate:[date dateByAddingTimeInterval:1] MoneyFor:Macro_MoneyForChange Remarks:@"取钱进来"];
        [WalletDefaults saveTotalAssets:self];
        return YES;
    } else {
        return NO;
    }
}
/*! @brief 卡收入 */
- (BOOL)saveCard:(NSString *)indexString Money:(float)money RecordDate:(NSDate *)date MoneyFor:(NSString *)moneyFor Remarks:(NSString *)remarks
{
    CardModel *card = [self cardWithIndexString:indexString];
    card.cardMoney = card.cardMoney + money;
    [[WalletLocalData sharedManager] addRecord:date MoneyFrom:card.cardName CheckoutType:YES Money:money MoneyFor:moneyFor Remarks:remarks];
    [WalletDefaults saveTotalAssets:self];
    return YES;
}

/*! @brief 钱包存入卡里 */
- (BOOL)purseToCard:(NSString *)indexString Money:(float)money RecordDate:(NSDate *)date
{
    if ([self drawWallemoney:money RecordDate:date MoneyFor:Macro_MoneyForChange Remarks:@"存到卡里"]) {
        [self saveCard:indexString Money:money RecordDate:[date dateByAddingTimeInterval:1] MoneyFor:Macro_MoneyForChange Remarks:@"转到卡里"];
        [WalletDefaults saveTotalAssets:self];
        return YES;
    } else {
        return NO;
    }
}


- (void)needUpTotalAssets
{
    if (self.delegate) {
        [self.delegate upTotalAssets:self Money:[self totalAssets]];
    }
}

@end
